package com.weitu.archives.personArchives.service.impl;

import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.archives.personArchives.entity.SecurityOrganizationLedger;
import com.weitu.archives.personArchives.entity.SecurityOrganizationLedgerRealation;
import com.weitu.archives.personArchives.mapper.SecurityOrganizationLedgerMapper;
import com.weitu.archives.personArchives.service.SecurityOrganizationLedgerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/5/15.
 */
@Service
public class SecurityOrganizationLedgerServiceImpl implements SecurityOrganizationLedgerService{
    private final SecurityOrganizationLedgerMapper securityOrganizationLedgerMapper;

    @Autowired
    public SecurityOrganizationLedgerServiceImpl(SecurityOrganizationLedgerMapper securityOrganizationLedgerMapper){
        this.securityOrganizationLedgerMapper=securityOrganizationLedgerMapper;
    }

    @Override
    public int queryCount(Map<String, Object> map) {
        return this.securityOrganizationLedgerMapper.queryCount(map);
    }

    @Override
    public List<SecurityOrganizationLedger> queryList(Map<String, Object> map) {
        return this.securityOrganizationLedgerMapper.queryList(map);
    }

    @Override
    @Transactional
    public boolean insertSecurityOrganizationLedger(SecurityOrganizationLedger securityOrganizationLedger, List<SecurityOrganizationLedgerRealation> securityOrganizationLedgerRealationList) {
        this.securityOrganizationLedgerMapper.deleteSecurityOrganizationLedgerRealation("");
        if(securityOrganizationLedgerRealationList.size()>0)
                this.securityOrganizationLedgerMapper.insertSecurityOrganizationLedgerRealaction(securityOrganizationLedgerRealationList);
        int count=this.securityOrganizationLedgerMapper.insertSecurityOrganizationLedger(securityOrganizationLedger);
        boolean flag= AccessoryUtil.saveAccerssoryInfo(securityOrganizationLedger.getId());
        boolean flag2=false;
        if(count==1){
            flag2=true;
        }
        return flag&&flag2;
    }

    @Override
    public SecurityOrganizationLedger querySecurityOrganizationLedger(String id) {
        return this.securityOrganizationLedgerMapper.querySecurityOrganizationLedger(id);
    }

    @Override
    @Transactional
    public boolean updateSecurityOrganizationLedger(SecurityOrganizationLedger securityOrganizationLedger,List<SecurityOrganizationLedgerRealation> realationList) {
        this.securityOrganizationLedgerMapper.deleteSecurityOrganizationLedgerRealation(securityOrganizationLedger.getId());
        if(realationList.size()>0)
            this.securityOrganizationLedgerMapper.insertSecurityOrganizationLedgerRealaction(realationList);
        int count=this.securityOrganizationLedgerMapper.updateSecurityOrganizationLedger(securityOrganizationLedger);
        boolean flag= AccessoryUtil.saveAccerssoryInfo(securityOrganizationLedger.getId());
        boolean flag2=false;
        if(count==1){
            flag2=true;
        }
        return flag&&flag2;
    }

    @Override
    public int deleteSecurityOrganizationLedger(String id) {
        return this.securityOrganizationLedgerMapper.deleteSecurityOrganizationLedger(id);
    }

    @Override
    public List<SecurityOrganizationLedgerRealation> queryListRelationById(String id) {
        return this.securityOrganizationLedgerMapper.queryListRelationById(id);
    }

    @Override
    public SecurityOrganizationLedgerRealation selectRelationById(String id) {
        return this.securityOrganizationLedgerMapper.selectRelationById(id);
    }

    @Override
    public int updateRelation(SecurityOrganizationLedgerRealation realation) {
        return this.securityOrganizationLedgerMapper.updateRelation(realation);
    }

    @Override
    public int insertRelation(SecurityOrganizationLedgerRealation realation) {
        return this.securityOrganizationLedgerMapper.insertRelation(realation);
    }
}
